<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="application/x-blueprint+xml;charset=UTF-8" language="java" %>

<%@ page import="com.vhly.bpplat.twitter.HTTPClient" %>
<%@ page import="com.vhly.bpplat.twitter.model.Status" %>
<%@ page import="com.vhly.bpplat.twitter.model.TwitterParser" %>
<%@ page import="org.kxml2.io.KXmlParser" %>
<%@ page import="org.kxml2.kdom.Document" %>
<%@ page import="org.xmlpull.v1.XmlPullParserException" %>
<%@ page import="java.io.ByteArrayInputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.net.URLEncoder" %>

<page xmlns:xsd="http://mobile.yahoo.com/widgets/schema/1.5/blueprint/">
    <content>
        <module>
            <header layout="simple">
                <layout-items>
                    <block>Friends Tweets</block>
                </layout-items>
            </header>
            <%
                String login = (String) session.getAttribute("login");
                if (login != null) {
                    String user = (String) session.getAttribute("user");
                    String pass = (String) session.getAttribute("pass");
                    String ftl = "http://twitter.com/statuses/friends_timeline.xml";
                    String acc = user + ":" + pass;
                    String xml = HTTPClient.request(ftl, acc, null, false);
                    if (xml != null) {
                        Document dom = new Document();
                        KXmlParser parser = new KXmlParser();
                        byte[] buf = xml.getBytes("UTF-8");
                        ByteArrayInputStream bin = null;
                        Vector<Status> sts = null;
                        try {
                            bin = new ByteArrayInputStream(buf);
                            parser.setInput(bin, null);
                            dom.parse(parser);
                            sts = TwitterParser.parseStatuses(dom.getRootElement());
                            for (Status st : sts) {
                                String stext = st.text;
            %>
            <module>
                <placard layout="simple" class="link">
                    <layout-items>
                        <block class="title"><%=st.user.screen_name%></block>
                        <block class="description"><%=stext%></block>
                        <block class="subtext"><%=st.create_at%></block>
                    </layout-items>
                        <%--<load event="activate" resource="info.jsp?u=<%=URLEncoder.encode(st.user.screen_name, "UTF-8")%>&amp;t=<%=URLEncoder.encode(stext, "UTF-8")%>"/>--%>
                </placard>
                <trigger>
                    <label>RT</label>
                    <load event="activate" resource="rtweet.jsp?tid=<%=st.id%>"/>
                </trigger>
                <trigger>
                    <label>Reply</label>
                    <load event="activate" resource="sendTweet.jsp?u=<%=URLEncoder.encode(st.user.screen_name,"UTF-8")%>&amp;rid=<%=st.id%>"/>
                </trigger>
            </module>
            <%
                        }
                    } catch (XmlPullParserException xe) {

                    } finally {
                        if (sts != null) {
                            sts.clear();
                        }
                        buf = null;
                        xml = null;
                        parser = null;
                        try {
                            bin.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        bin = null;
                    }
                }
            } else {%>
            <block>Need Login</block>
            <% } %>
        </module>
    </content>

</page>
